package org.wkg.lesson09;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class MyThreadPool02 {

    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue();

    public MyThreadPool02(int NUM) {
        if (NUM <= 0) {
            throw new IllegalArgumentException("线程数不可以小于等于0");
        }
        for (int i = 0; i < NUM; i++) {
            Thread thread = new Thread(() -> {
                while (true) {
                    try {
                        Runnable task = queue.take();
                        task.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
        }
    }


    public void schule(Runnable runnable) throws InterruptedException {
        if (runnable == null) {
            throw new IllegalArgumentException("线程任务不能为null");
        }
        queue.put(runnable);
    }

}
